const Router = require("koa-router");
const { redis } = require("@core/redis");
const { Activity } = require("@models/activity");
const { Enroll } = require("@models/enroll");
const { Auth } = require("@middlewares/auth");
const { User } = require("@models/user");
const { Admin } = require("@models/admin");
const { Op } = require("sequelize");

const router = new Router({
  prefix: "/admin/activity",
});

// 拉取活动列表
router.post("/list", new Auth(16).m, async (ctx, next) => {
  const { page = 1, pageSize = 10, title = "" } = ctx.request.body;
  const offset = (page - 1) * pageSize;
  const where = {
    title: { [Op.like]: `%${title}%` },
  };
  const { count: total, rows } = await Activity.findAndCountAll({
    where,
    limit: pageSize, // 每页数量
    offset, // 偏移量
    order: [
      ["sort", "desc"],
      ["created_at", "DESC"],
    ], // 排序
    include: [
      {
        model: Enroll,
        required: false,
      },
    ],
  });
  ctx.body = {
    code: 200,
    msg: "获取成功",
    total,
    data: rows,
  };
});

// 删除活动

router.post("/delete", new Auth(16).m, async (ctx, next) => {
  const { activity_id } = ctx.request.body;
  const where = {
    activity_id,
  };
  const activity = await Activity.destroy({
    where,
  });
  if (activity == 0) {
    throw new global.errs.AuthFailed("活动删除失败");
  }
  await redis.del(`activity:${activity_id}`);
  ctx.body = {
    code: 200,
    msg: "删除成功",
  };
});

// 创建活动

router.post("/create", new Auth(16).m, async (ctx, next) => {
  const { enrollment, activity_id, admin_id, ...res } = ctx.request.body;

  const activity = await Activity.create({
    ...res,
    admin_id: ctx.auth.uid,
  });
  if (!activity) {
    throw new global.errs.AuthFailed("活动创建失败");
  }

  ctx.body = {
    code: 200,
    msg: "创建成功",
    data: activity,
  };
});

// 更新活动
router.post("/update", new Auth(16).m, async (ctx, next) => {
  const { admin_id, activity_id, ...data } = ctx.request.body;

  const activity = await Activity.update(data, {
    where: {
      activity_id,
    },
  });

  if (activity[0] == 0) {
    throw new global.errs.AuthFailed("活动更新失败");
  }
  await redis.del(`activity:${activity_id}`);
  ctx.body = {
    code: 200,
    msg: "更新成功",
    data: activity,
  };
});

// 活动上架下架
router.post("/isDestroy", new Auth(16).m, async (ctx, next) => {
  const { activity_id, is_destroy } = ctx.request.body;

  const activity = await Activity.update(
    {
      is_destroy: is_destroy,
    },
    {
      where: { activity_id },
    }
  );
  await redis.del(`activity:${activity_id}`);
  if (activity[0]) {
    ctx.body = {
      msg: is_destroy ? "上架成功" : "下架成功",
      code: 200,
      data: {},
    };
  } else {
    throw new global.errs.AuthFailed(is_destroy ? "上架失败" : "下架失败");
  }
});

// 查询报名活动的用户
router.post("/enroll/user", new Auth(16).m, async (ctx, next) => {
  const {
    page = 1,
    pageSize = 10,
    activity_id,
    phone = "",
    enroll_number = "",
  } = ctx.request.body;

  const offset = (page - 1) * pageSize;
  const { count: total, rows } = await Enroll.findAndCountAll({
    limit: pageSize, // 每页数量
    offset, // 偏移量
    where: { activity_id, enroll_number: { [Op.like]: `%${enroll_number}%` } },
    include: [
      { model: User, where: { phone: { [Op.like]: `%${phone}%` } } },
      {
        model: Admin,
        attributes: ["admin_id", "admin_name", "avatar"],
      },
    ],
  });

  ctx.body = {
    msg: "查询成功",
    code: 200,
    total,
    data: rows,
  };
});

module.exports = router;
